package com.cal.utms.model.dealing;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.cal.utms.action.user_maintenance.LoginAction;
import com.cal.utms.dao.dealing.InstrumentDao;
import com.cal.utms.dao.maintenance.DealTypeDao;
import com.cal.utms.dao.maintenance.IssuerDaoImpl;
import com.cal.utms.dao.maintenance.MasterDataDaoImpl;
import com.cal.utms.dao.maintenance.NewFundDao;
import com.cal.utms.dto.dealing.InstrumentDto;
import com.cal.utms.dto.dealing.PurchaseDealDto;
import com.cal.utms.entity.Deal;
import com.cal.utms.entity.DealInstrumentMap;
import com.cal.utms.entity.Dealer;
import com.cal.utms.entity.DelevaryMethod;
import com.cal.utms.entity.Instrument;
import com.cal.utms.entity.InstrumentCouponDate;
import com.cal.utms.entity.InstrumentType;
import com.cal.utms.entity.SettlementMethod;
import com.cal.utms.enumType.InterestRate;
import com.cal.utms.enumType.InterestType;
import com.cal.utms.util.Sequence;
import com.cal.utms.valuation.PurchaseCalculator;

public class PurchaseDealModel {
	// creteor, date
	// dealCode
	// status
	public Deal createDtoToDeal(PurchaseDealDto dealDto, InstrumentType insType) {
		Deal deal = new Deal();
		List<DealInstrumentMap> dimList = new ArrayList<DealInstrumentMap>();
		deal.setDealInstrumentMaps(dimList);
		Sequence seq = new Sequence();
		String dealCode = "PC" + seq.getNextValue("DealSequence");

		deal.setCreatedDate(new Date());
		deal.setCreatedUser(LoginAction.getCurrentLogingUser().getUserName());
		deal.setStatus("Initial");
		deal.setDealCode(dealCode);
		dealDto.setDealCode(dealCode);

		return mapDtoToDeal(dealDto, insType, deal);
	}

	public Deal confirmtoToDeal(PurchaseDealDto dealDto, InstrumentType insType) {
		Deal deal = new Deal();
		List<DealInstrumentMap> dimList = new ArrayList<DealInstrumentMap>();
		deal.setDealInstrumentMaps(dimList);
		return mapDtoToDeal(dealDto, insType, deal);
	}

	public Deal updateDtoToDeal(PurchaseDealDto dealDto, InstrumentType insType, Deal deal) {
		System.out.println(new Date());
		deal.setModifiedDate(new Date());
		deal.setModifiedUser(LoginAction.getCurrentLogingUser().getUserName());
		return mapDtoToDeal(dealDto, insType, deal);
	}

	public Deal mapDtoToDeal(PurchaseDealDto dealDto, InstrumentType insType, Deal deal) {

		IssuerDaoImpl issuerDao = new IssuerDaoImpl();
		Number number;
		NumberFormat format = NumberFormat.getInstance();

		DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
		Date result = null;
		if (dealDto.getCouponDates() != null && !dealDto.getCouponDates().equals(""))
			for (String cpnDate : dealDto.getCouponDates().split(";")) {
				try {
					result = df.parse(cpnDate);
				} catch (ParseException e) {
					System.out.println("Invalid Coupon date:" + cpnDate + " for deal:" + dealDto.getName());
					e.printStackTrace();
				}
			}
		deal.setActualRate(new BigDecimal(dealDto.getRateinterest().replace("%", "")));
		if (dealDto.getSecSellerBankNo() != null && !dealDto.getSecSellerBankNo().equals(""))
			deal.setBankAccount(dealDto.getSecSellerBankNo());

		if (dealDto.getDayBasis() == null) {
			deal.setDayBasis(insType.getDayBasis());
		} else {
			deal.setDayBasis(dealDto.getDayBasis());
		}

		try {
			number = format.parse(dealDto.getDealValue());
			deal.setDealValue(new BigDecimal(number.doubleValue()));
		} catch (ParseException e) {
			System.out.println("Error in parsing Deal Value:" + dealDto.getDealValue());
			dealDto.setSuccess(false);
			dealDto.setStatus("Invalid Deal Value!");
			return deal;
		}

		if (dealDto.getMaturityValue() != null) {
			try {
				number = format.parse(dealDto.getMaturityValue());
				deal.setMaturityValue(new BigDecimal(number.doubleValue()));
			} catch (ParseException e) {
				System.out.println("Error in parsing Maturity Value:" + dealDto.getMaturityValue());
				dealDto.setSuccess(false);
				dealDto.setStatus("Invalid Maturity Value!");
				return deal;
			}
		}

		try {
			result = df.parse(dealDto.getValueDate());
			deal.setValueDate(result);
		} catch (ParseException e) {
			System.out.println("Invalid Value:" + dealDto.getValueDate() + " for deal:" + dealDto.getName());
			e.printStackTrace();
			dealDto.setSuccess(false);
			dealDto.setStatus("Invalid Deal Value Date!");
			return deal;
		}
		try {
			result = df.parse(dealDto.getMaturityDate());
			deal.setMaturityDate(result);
		} catch (ParseException e) {
			System.out.println("Invalid Coupon date:" + dealDto.getMaturityDate() + " for deal:" + dealDto.getName());
			e.printStackTrace();
			dealDto.setSuccess(false);
			dealDto.setStatus("Invalid Deal Maturity Date!");
			return deal;
		}

		deal.setName(dealDto.getName());
		if (dealDto.getPlacementFee() != null && !dealDto.getPlacementFee().trim().equals("")) {
			try {
				number = format.parse(dealDto.getPlacementFee());
				deal.setPlacementFee(new BigDecimal(number.doubleValue()));
			} catch (ParseException e) {
				System.out.println("Error in parsing Placement Fee1:" + dealDto.getPlacementFee());
				dealDto.setSuccess(false);
				dealDto.setStatus("Invalid Placement Fee!");
				return deal;
			}
		}
		deal.setSecondaryMarket((byte) dealDto.getSecMarket());

		if (dealDto.getSecSellerName() != null && !dealDto.getSecSellerName().equals("")) {
			deal.setSellerName(dealDto.getSecSellerName());
		}
		if (dealDto.getSecSellerBankNo() != null && !dealDto.getSecSellerBankNo().equals("")) {
			deal.setBankAccount(dealDto.getSecSellerBankNo());
		}

		if (dealDto.getWithholdingTax() != null) {
			deal.setWithholdingTax(dealDto.getWithholdingTax());
		} else {
			deal.setWithholdingTax(insType.getWithholdingTax().toString());
		}

		if (dealDto.getWithholdingTaxRate() != null && !dealDto.getWithholdingTaxRate().equals("")) {
			System.out.println("WHTR:" + dealDto.getWithholdingTaxRate().substring(0, dealDto.getWithholdingTaxRate().length() - 1));
			deal.setWithholdingTaxRate(new BigDecimal(dealDto.getWithholdingTaxRate().substring(0, dealDto.getWithholdingTaxRate().length() - 1)));
			deal.setWithholdingCertificate("NotReceived");
		} else {
			deal.setWithholdingCertificate("No");
		}

		deal.setDealType(new DealTypeDao().getDealTypeByCode("PURCH"));
		deal.setFund(new NewFundDao().getById(dealDto.getFund()));

		if (dealDto.getPrimaryDealer() > 0) {
			deal.setIssuer(issuerDao.getById(dealDto.getPrimaryDealer()));
		} else if (dealDto.getIssuer() > 0) {
			deal.setIssuer(issuerDao.getById(dealDto.getIssuer()));
		} else {
			dealDto.setSuccess(false);
			dealDto.setStatus("Invalid Issuer!");
			return deal;
		}
		MasterDataDaoImpl masterDataDaoImpl = new MasterDataDaoImpl();
		if (dealDto.getSettlementCode() != 0) {
			SettlementMethod method = masterDataDaoImpl.getSettlementById(dealDto.getSettlementCode());
			if (method != null) {
				System.out.println(method.getCode());
				deal.setSettlementMethod(method);
			} else {
				dealDto.setSuccess(false);
				dealDto.setStatus("Invalid Settlement Methode!");
				return deal;
			}
		}
		if (dealDto.getDelevarymethode() != 0) {
			DelevaryMethod delevaryMethod = masterDataDaoImpl.getDelevaryMethodById(dealDto.getDelevarymethode());
			if (delevaryMethod != null)
				deal.setDelevarymethode(delevaryMethod);
			else {
				dealDto.setSuccess(false);
				dealDto.setStatus("Invalid Delevary Methode!");
				return deal;
			}
		}

		if (dealDto.getDealerCode() != 0) {
			Dealer dealer = masterDataDaoImpl.getdealerByid(dealDto.getDealerCode());
			if (dealer != null)
				deal.setDealerCode(dealer);
			else {
				dealDto.setSuccess(false);
				dealDto.setStatus("Invalid Dealer!");
				return deal;
			}
		}
		if (dealDto.getParams() != null && !dealDto.getParams().trim().equals("")) {
			deal.setRolloverShortmaturity(dealDto.getParams());
		}

		return deal;
	}

	public PurchaseDealDto mapDealToDto(Deal deal) {
		PurchaseDealDto dealDto = new PurchaseDealDto();
		NumberFormat nf = new DecimalFormat("###,##0.0000");
		NumberFormat ratenf = new DecimalFormat("###,##0.00");
		// DateFormat dfLong = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

		dealDto.setId(deal.getId());
		dealDto.setStatus(deal.getStatus());
		dealDto.setName(deal.getName());
		if (deal.getDealInstrumentMaps().isEmpty()) {
			dealDto.setStatus("Cannot Find the Instrument!");
			dealDto.setSuccess(false);
			return dealDto;
		} else {
			dealDto.setInsType(deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentType().getId());
			if (deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentType().getListed() == 0) {
				dealDto.setInsCode(deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentCode());
			} else {
				dealDto.setListedInsCode(deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentCode());
			}
			if (deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentCouponDates() != null) {
				String cpnDates = "";
				for (InstrumentCouponDate icd : deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentCouponDates()) {
					cpnDates = cpnDates + df.format(icd.getCouponDate()) + ";";
				}
				dealDto.setCouponDates(cpnDates);
			}
			if (deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentType().getIstrumentType() == com.cal.utms.enumType.InstrumentType.G) {
				dealDto.setPrimaryDealer(deal.getIssuer().getId());
			} else {
				dealDto.setIssuer(deal.getIssuer().getId());
			}

			if (deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentType().getCouponAvailability() != 0) {
				if (deal.getDealInstrumentMaps().get(0).getInstrument().getCouponRate() != null)
					dealDto.setCouponRate(ratenf.format(deal.getDealInstrumentMaps().get(0).getInstrument().getCouponRate().doubleValue()) + "%");
				if (deal.getDealInstrumentMaps().get(0).getInstrument().getCouponFreq() != null)
					dealDto.setCouponFreq(deal.getDealInstrumentMaps().get(0).getInstrument().getCouponFreq());
			}

			if (deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentType().getInterestRate() == InterestRate.V) {
				dealDto.setRateResetPeriod(deal.getDealInstrumentMaps().get(0).getInstrument().getResetPeriod());
				dealDto.setResetDate(df.format(deal.getDealInstrumentMaps().get(0).getInstrument().getResetDate()));
				dealDto.setBenchMark(deal.getDealInstrumentMaps().get(0).getInstrument().getBenchMark());
				dealDto.setFloorValue(deal.getDealInstrumentMaps().get(0).getInstrument().getFloor().doubleValue());
				dealDto.setCeilingValue(deal.getDealInstrumentMaps().get(0).getInstrument().getCeiling().doubleValue());
			}

			if (deal.getDealInstrumentMaps().get(0).getInstrument().getInstrumentType().getDenominals() != 0) {
				dealDto.setDenominalMinVal(deal.getDealInstrumentMaps().get(0).getInstrument().getMinDenominal().doubleValue());
			}
		}

		dealDto.setDealValue(nf.format(deal.getDealValue()));
		dealDto.setValueDate(df.format(deal.getValueDate()));
		if (deal.getPlacementFee() != null)
			dealDto.setPlacementFee(nf.format(deal.getPlacementFee()));
		dealDto.setSecMarket(deal.getSecondaryMarket());
		if (deal.getSellerName() != null)
			dealDto.setSecSellerName(deal.getSellerName());
		if (deal.getBankAccount() != null)
			dealDto.setSecSellerBankNo(deal.getBankAccount());

		dealDto.setRateinterest(ratenf.format(deal.getActualRate().doubleValue()) + "%");
		dealDto.setWithholdingTax(deal.getWithholdingTax());
		if (deal.getWithholdingTaxRate() != null)
			dealDto.setWithholdingTaxRate(ratenf.format(deal.getWithholdingTaxRate().doubleValue()) + "%");
		dealDto.setDayBasis(deal.getDayBasis());
		dealDto.setDealCode(deal.getDealCode());
		dealDto.setFund(deal.getFund().getId());
		dealDto.setMaturityDate(df.format(deal.getMaturityDate()));
		dealDto.setValueDate(df.format(deal.getValueDate()));
		if (deal.getRolloverShortmaturity() != null && !deal.getRolloverShortmaturity().trim().equals("")) {
			dealDto.setParams(deal.getRolloverShortmaturity());
		}
		if (deal.getMaturityValue() != null) {
			dealDto.setMaturityValue(nf.format(deal.getMaturityValue()));
		} else {
			dealDto.setMaturityValue("0000.0000");
		}

		if (deal.getSettlementMethod() != null) {
			dealDto.setSettlementCode(deal.getSettlementMethod().getId());
		}
		if (deal.getDelevarymethode() != null) {
			dealDto.setDelevarymethode(deal.getDelevarymethode().getId());
		}
		if (deal.getDealerCode() != null) {
			dealDto.setDealerCode(deal.getDealerCode().getId());
		}
		return dealDto;
	}

	public PurchaseDealDto evaluatePurchase(PurchaseDealDto dealDto, InstrumentType insType) {
		PurchaseCalculator purchase = new PurchaseCalculator();
		String wht = null;
		if (dealDto.getWithholdingTax() != null) {
			wht = dealDto.getWithholdingTax();
		} else {
			wht = insType.getWithholdingTax().toString();
		}
		System.out.println("DTO: " + dealDto.getWithholdingTax());
		System.out.println("IntType: " + insType.getWithholdingTax());
		double dealValue;
		double rate = Double.parseDouble(dealDto.getRateinterest().replace("%", ""));
		double couponRate = dealDto.getCouponRate() != null ? Double.parseDouble(dealDto.getCouponRate().replace("%", "")) : 0;
		double whtRate = 0;
		Date purchaseDate = null;
		Date maturityDate = null;
		int dayCount;

		Number number;
		NumberFormat format = NumberFormat.getInstance();
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
		NumberFormat nf = new DecimalFormat("###,###.####");

		try {
			number = format.parse(dealDto.getDealValue());
			dealValue = number.doubleValue();
		} catch (ParseException pe) {
			System.out.println("DealValue parse Error: " + dealDto.getDealValue());
			dealValue = 0;
			dealDto.setSuccess(false);
			dealDto.setStatus("DealValue parse Error: " + dealDto.getDealValue());
		}
		number = null;
		try {
			purchaseDate = df.parse(dealDto.getValueDate());
		} catch (ParseException pe) {
			System.out.println("Value Date parse error: " + dealDto.getValueDate());
			purchaseDate = new Date();
			dealDto.setSuccess(false);
			dealDto.setStatus("Value Date parse error: " + dealDto.getValueDate());
		}
		try {
			maturityDate = df.parse(dealDto.getMaturityDate());
		} catch (ParseException pe) {
			System.out.println("Maturity date parse error: " + dealDto.getMaturityDate());
			maturityDate = new Date();
			dealDto.setSuccess(false);
			dealDto.setStatus("Maturity date parse error: " + dealDto.getMaturityDate());
		}
		if (dealDto.getDayBasis().equals("Act / 360")) {
			dayCount = 360;
		} else if (dealDto.getDayBasis().equals("Act / 364")) {
			dayCount = 364;
		} else if (dealDto.getDayBasis().equals("30 / 360")) {
			dayCount = 360;
		} else {
			dayCount = 365;
		}

		if (wht.equals("Upfront") || wht.equals("Maturity")) {
			try {
				number = format.parse(dealDto.getWithholdingTaxRate());
				whtRate = number.doubleValue();
			} catch (ParseException pe) {
				whtRate = 0;
				System.out.println("Withholding Tax Rate Parse Error :" + dealDto.getWithholdingTaxRate() + " : " + pe.getMessage());
				dealDto.setSuccess(false);
				dealDto.setStatus("Withholding Tax Rate Parse Error :" + dealDto.getWithholdingTaxRate());
			}
		}

		System.out.println("INSTYPE: " + insType.getInterestRate() + " : " + insType.getInterestType() + " : " + insType.getCouponAvailability() + " : " + wht);
		if (!dealDto.isSuccess()) {
			System.out.println("PurchaseCalculator Evaluation Failed : " + dealDto.getStatus());
		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 0 && wht.equals("No")) {
			System.out.println("FSNN" + dealValue + ":" + rate + ":" + purchaseDate + ":" + maturityDate + ":" + dayCount);
			purchase.valuateCpaFaceValueFsnn(dealValue, rate, purchaseDate, maturityDate, dayCount);
			dealDto.setMaturityValue(nf.format(purchase.getFaceValue()));
		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 0 && wht.equals("Upfront")) {
			System.out.println("FSNU" + dealValue + ":" + rate + ":" + purchaseDate + ":" + maturityDate + ":" + dayCount);
			purchase.valuateCpaFaceValueFsnu(dealValue, rate, purchaseDate, maturityDate, dayCount, whtRate);
			dealDto.setMaturityValue(purchase.getFaceValue() + "");
		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 0 && wht.equals("Maturity")) {
			System.out.println("FSNM" + dealValue + ":" + rate + ":" + purchaseDate + ":" + maturityDate + ":" + dayCount);
			purchase.valuateCpaFaceValueFsnm(dealValue, rate, purchaseDate, maturityDate, dayCount, whtRate);
			dealDto.setMaturityValue(purchase.getFaceValue() + "");
		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 1 && wht.equals("No")) {
			System.out.println("FSY1N" + dealValue + ":" + rate + ":" + purchaseDate + ":" + maturityDate + ":" + dayCount);
			purchase.valuateCpaFaceValueFsyn(dealValue, rate, couponRate, purchaseDate, maturityDate, dayCount);
			dealDto.setMaturityValue(purchase.getFaceValue() + "");
		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 2 && wht.equals("No")) {
			System.out.println("FSY2N" + dealValue + ":" + rate + ":" + purchaseDate + ":" + maturityDate + ":" + dayCount);
			purchase.valuateCpaFaceValueFsyn(dealValue, rate, couponRate, purchaseDate, maturityDate, dayCount);
			dealDto.setMaturityValue(purchase.getFaceValue() + "");
		} else {
			System.out.println("Other" + dealValue + ":" + rate + ":" + purchaseDate + ":" + maturityDate + ":" + dayCount);
			purchase.valuateCpaFaceValueFsnn(dealValue, rate, purchaseDate, maturityDate, dayCount);
			dealDto.setMaturityValue(purchase.getFaceValue() + "");
		}
		return dealDto;
	}

	public List<InstrumentDto> getInstrumentByType(int instrumentTypeId) {
		NumberFormat nf = new DecimalFormat("###,###.00");
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		InstrumentDao dao = new InstrumentDao();
		List<Instrument> instruments = dao.getInstrumentByType(instrumentTypeId);
		List<InstrumentDto> instrumentDtos = new ArrayList<InstrumentDto>();
		if (instruments != null) {
			for (Instrument instrument : instruments) {
				InstrumentDto dto = new InstrumentDto();
				if (instrument.getInstrumentType().getCouponAvailability() == 0) {
					dto.setCouponRate("0.00%");
				} else {
					dto.setCouponRate(nf.format(instrument.getCouponRate()) + "%");
				}
				dto.setDayBasis(instrument.getDayBasis());
				dto.setId(instrument.getId());
				dto.setListedInsCode(instrument.getInstrumentCode());
				dto.setValueDate(df.format(instrument.getIssueDate()));
				dto.setIssuer(instrument.getIssuer().getId());
				dto.setIssuerName(instrument.getIssuer().getCode());
				dto.setLabel(instrument.getLabel());
				dto.setMaturityDate(df.format(instrument.getMaturityDate()));
				dto.setMinDenominal(Integer.toString(instrument.getMinDenominal().intValue()));
				instrumentDtos.add(dto);
			}
		}
		return instrumentDtos;
	}

	public PurchaseDealDto evaluatePurchaseDealValue(PurchaseDealDto dealDto, InstrumentType insType) {
		PurchaseCalculator purchase = new PurchaseCalculator();
		String wht = null;
		if (dealDto.getWithholdingTax() != null) {
			wht = dealDto.getWithholdingTax();
		} else {
			wht = insType.getWithholdingTax().toString();
		}

		double maturityvalue;
		double rate = Double.parseDouble(dealDto.getRateinterest().replace("%", ""));
		double couponRate = dealDto.getCouponRate() != null ? Double.parseDouble(dealDto.getCouponRate().replace("%", "")) : 0;
		double whtRate = 0;
		Date purchaseDate = null;
		Date maturityDate = null;
		int dayCount;

		Number number;
		NumberFormat format = NumberFormat.getInstance();
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
		NumberFormat nf = new DecimalFormat("###,###.####");

		try {
			number = format.parse(dealDto.getMaturityValue());
			maturityvalue = number.doubleValue();
		} catch (ParseException pe) {
			System.out.println("DealValue parse Error: " + dealDto.getMaturityValue());
			maturityvalue = 0;
			dealDto.setSuccess(false);
			dealDto.setStatus("DealValue parse Error: " + dealDto.getMaturityValue());
		}
		number = null;
		try {
			purchaseDate = df.parse(dealDto.getValueDate());
		} catch (ParseException pe) {
			System.out.println("Value Date parse error: " + dealDto.getValueDate());
			purchaseDate = new Date();
			dealDto.setSuccess(false);
			dealDto.setStatus("Value Date parse error: " + dealDto.getValueDate());
		}
		try {
			maturityDate = df.parse(dealDto.getMaturityDate());
		} catch (ParseException pe) {
			System.out.println("Maturity date parse error: " + dealDto.getMaturityDate());
			maturityDate = new Date();
			dealDto.setSuccess(false);
			dealDto.setStatus("Maturity date parse error: " + dealDto.getMaturityDate());
		}
		if (dealDto.getDayBasis().equals("Act / 360")) {
			dayCount = 360;
		} else if (dealDto.getDayBasis().equals("Act / 364")) {
			dayCount = 364;
		} else if (dealDto.getDayBasis().equals("30 / 360")) {
			dayCount = 360;
		} else {
			dayCount = 365;
		}

		if (wht.equals("Upfront") || wht.equals("Maturity")) {
			try {
				number = format.parse(dealDto.getWithholdingTaxRate());
				whtRate = number.doubleValue();
			} catch (ParseException pe) {
				whtRate = 0;
				System.out.println("Withholding Tax Rate Parse Error :" + dealDto.getWithholdingTaxRate() + " : " + pe.getMessage());
				dealDto.setSuccess(false);
				dealDto.setStatus("Withholding Tax Rate Parse Error :" + dealDto.getWithholdingTaxRate());
			}
		}

		if (!dealDto.isSuccess()) {
			System.out.println("PurchaseCalculator Evaluation Failed : " + dealDto.getStatus());
		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 0 && wht.equals("No")) {
			System.out.println("FSNN" + maturityvalue + ":" + rate + ":" + purchaseDate + ":" + maturityDate + ":" + dayCount);
			purchase.valuateCpaDealValueFsnn(maturityvalue, rate, purchaseDate, maturityDate, dayCount);
			dealDto.setDealValue(nf.format(purchase.getPrvalBeforeWhtd()));
		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 0 && wht.equals("Upfront")) {

		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 0 && wht.equals("Maturity")) {

		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 1 && wht.equals("No")) {

		} else if (insType.getInterestRate() == InterestRate.F && insType.getInterestType() == InterestType.S && insType.getCouponAvailability() == 2 && wht.equals("No")) {

		} else {

		}
		return dealDto;
	}

}
